package cn.edu.zafu.bigdata.wupeng;

import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        System.out.println("欢迎来到猜数字游戏！");
        System.out.print("请输入你想要尝试的最大次数（至少1次）：");
        int maxTries = scanner.nextInt();

        if (maxTries < 1) {
            System.out.println("最大次数至少为1，请重新运行程序并输入有效数字。");
            scanner.close();
            return;
        }

        int numberToGuess = random.nextInt(100) + 1; // 生成一个1到100之间的随机数
        int guess = 0;
        int tries = 0;

        System.out.println("我已经想好了一个1到100之间的数字，你有" + maxTries + "次机会猜中它。");

        while (tries < maxTries) {
            System.out.print("猜一猜: ");
            guess = scanner.nextInt();
            tries++;

            if (guess == numberToGuess) {
                System.out.println("恭喜你！你猜对了，数字就是" + numberToGuess + "。");
                System.out.println("你总共猜了" + tries + "次。");
                break;
            } else {
                if (guess < numberToGuess) {
                    System.out.println("太小了，再试试。");
                } else if (guess > numberToGuess) {
                    System.out.println("太大了，再试试。");
                }
            }
        }

        if (guess != numberToGuess) {
            System.out.println("很遗憾，你的猜测次数已用完。我想的数字是" + numberToGuess + "。");
        }

        scanner.close();
    }
}